//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    titleBarHeight:'',
    tabs: [],
    activeTab: 0,
    list:[],
    offset:0,
    showLoading:true,
    searchData: '',
    pushOver: false
  },
  onLoad: function () {
    var that = this;
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log(data)
      that.getData(data.data,data.typeId)
      that.setData({activeTab: data.typeId,searchData: data.data})
    })

    wx.getSystemInfo({
      success: function(res) {
        that.setData({titleBarHeight: res.statusBarHeight + 46})
      }
    })

    const titles = ['威士忌', '品牌', '酒厂', '装瓶商']
    const tabs = titles.map(item => ({title: item}))
    console.log(tabs)
    this.setData({tabs})
  },
  onTabCLick(e) {
    const index = e.detail.index
    //切换栏目，重置搜索，重置数据
    this.setData({activeTab: index,showLoading: true,list: [],offset: 0})
    setTimeout((item)=>{
      this.getData(this.data.searchData, index)
    },500)
    
  },

  onChange(e) {
    // const index = e.detail.index
    // this.setData({activeTab: index})
  },
  navBack(){
    wx.navigateBack({
      delta: 1
    })    
  },
  getData(data, typeId){
    let that = this
    if(that.data.pushOver) return
    let datas = {
      target: data,
      num: 20,
      offset: that.data.offset
    }
    let typeArr = ['whisky','brand','distillery','business']
    app.request({
      url: '/' + typeArr[typeId] + '/search-list',
      data: datas,
      success: function(res) {
        console.log(res.data.list)
        if(res && res.data && !res.data.list)
        {
          that.setData({showLoading: false})
          return
        }
        if(that.data.offset > res.data.total_page)
        {
          that.setData({pushOver:true})
          return
        }
        that.setData({list: that.data.list.concat(res.data.list), offset: that.data.offset + 1})

        // that.setData({list: that.data.list.concat(res.data.list)})
        that.setData({showLoading: false})
      }
    })
  },
  goSearch(e){
    if(e && e.detail && e.detail.value){
      this.setData({searchData: e.detail.value,showLoading: true,list: [],offset: 0})
      this.getData(e.detail.value, this.data.activeTab)
      return
    }
    wx.showToast({
      title: '请输入内容',
      icon: 'none'
    })
    this.setData({searchData: ''})
  },
  goDes(e){
    console.log(e)
    let data = e.currentTarget.dataset.item
    data.typeId = this.data.activeTab
    console.log(data.typeId)
    if(data.typeId >= 1)
    {
      wx.navigateTo({
        url: '/pages/details/index?id=1' + '&name='+e.currentTarget.dataset.item.name,
        // success: function(res) {
        //   // 通过eventChannel向被打开页面传送数据
        //   res.eventChannel.emit('acceptDataFromOpenerPage', data)
        // }
      })
    }else{ 
      wx.navigateTo({
        url: '/pages/works/index?id=' + e.currentTarget.dataset.item.wineid,
     
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触底')
    this.getData(this.data.searchData, this.data.activeTab)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
